﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;
using System.Xml;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;

namespace SSISForSAPComponents
{

    /// <summary>
    /// SAP连接管理器
    /// </summary>
    [Microsoft.SqlServer.Dts.Runtime.DtsConnection(DisplayName = "SAPConnection", ConnectionType = "SAP", Description = "Connection to SAP R/3")]
    public class SAPConnectionManager :ConnectionManagerBase, IDTSComponentPersist
    {
        
      
        //private string _ConnectionString = "";
        private string _CLIENT;
        /// <summary>
        /// 客户端
        /// </summary>
        public string CLIENT
        {
            get { return _CLIENT; }
            set { _CLIENT = value; }
        }
        private string _USER="";

        /// <summary>
        /// user
        /// </summary>
        public string USER
        {
            get { return _USER; }
            set { _USER = value; }
        }
        private string _PASSWD = "";

        /// <summary>
        /// 密码
        /// </summary>
        public string PASSWD
        {
            get { return _PASSWD; }
            set { _PASSWD = value; }
        }
        private string _lang = "";

        /// <summary>
        /// 语言
        /// </summary>
        public string Lang
        {
            get { return _lang; }
            set { _lang = value; }
        }
        private string _ASHOST = "";

        /// <summary>
        /// 主机
        /// </summary>
        public string ASHOST
        {
            get { return _ASHOST; }
            set { _ASHOST = value; }
        }
        private string _SYSNR = "";

        /// <summary>
        /// 系统编号
        /// </summary>
        public string SYSNR
        {
            get { return _SYSNR; }
            set { _SYSNR = value; }
        }
        public override object AcquireConnection(object txn)
        {
                 
            return this;
        }

        public override   string ConnectionString
        {
            get
            {
                return "CLIENT=" + this._CLIENT + " USER=" + this._USER + " PASSWD=" + this._PASSWD + " lang="+this._lang+" ASHOST="+this._ASHOST +" SYSNR="+this._SYSNR ;
            }
            set
            {
                string[] Pas = value.Split(new string[] { " " }, StringSplitOptions.None );
               // System.Collections.Generic.Dictionary<string, string> list = new System.Collections.Generic.Dictionary<string, string>();
                for (int i = 0; i < Pas.Length; i++)
                {
                   
                    string key = Pas[i].Split(new string[] { "=" }, StringSplitOptions.None)[0];
                    string svalue = Pas[i].Split(new string[] { "=" }, StringSplitOptions.None)[1];
                    switch (key)
                    {
                        case "CLIENT":
                            this._CLIENT = svalue;
                            break;
                        case "USER":
                            this._USER = svalue;
                            break;
                        case "PASSWD":
                            this._PASSWD = svalue;
                            break;
                        case "lang":
                            this._lang = svalue;
                            break;
                        case "ASHOST":
                            this._ASHOST = svalue;
                            break;
                        case "SYSNR":
                            this._SYSNR = svalue;
                            break;
                        default:
                            break;
                    }
                }
            }
        }




        #region IDTSComponentPersist 成员

        public void LoadFromXML(XmlElement node, IDTSInfoEvents infoEvents)
        {
            if (node.Name == "SAPConnectionManager")
            {
                XmlNode CLIENTItem = node.Attributes.GetNamedItem("CLIENT");
                if (CLIENTItem != null)
                {
                    this._CLIENT  = CLIENTItem.Value;
                }
                XmlNode USERItem = node.Attributes.GetNamedItem("USER");
                if (USERItem != null)
                {
                    this._USER  = USERItem.Value;
                }

                XmlNode PASSWDItem = node.Attributes.GetNamedItem("PASSWD");
                if (PASSWDItem != null)
                {
                    this._PASSWD = PASSWDItem.Value;
                }

                XmlNode LangItem = node.Attributes.GetNamedItem("Lang");
                if (LangItem != null)
                {
                    this._lang = LangItem.Value;
                }

                XmlNode ASHOSTItem = node.Attributes.GetNamedItem("ASHOST");
                if (ASHOSTItem != null)
                {
                    this._ASHOST = ASHOSTItem.Value;
                }

                XmlNode SYSNRItem = node.Attributes.GetNamedItem("SYSNR");
                if (SYSNRItem != null)
                {
                    this._SYSNR = SYSNRItem.Value;
                }
            }
        }

        public void SaveToXML(XmlDocument doc, IDTSInfoEvents infoEvents)
        {
            XmlElement newChild = doc.CreateElement("", "SAPConnectionManager", "");
            doc.AppendChild(newChild);
          

            XmlAttribute CLIENT = doc.CreateAttribute("CLIENT");
            CLIENT.Value = this._CLIENT;
            newChild.Attributes.Append(CLIENT);

            XmlAttribute USER = doc.CreateAttribute("USER");
            USER.Value = this._USER;
            newChild.Attributes.Append(USER);

            XmlAttribute PASSWD = doc.CreateAttribute("PASSWD");
            PASSWD.Value = this._PASSWD;
            newChild.Attributes.Append(PASSWD);

            XmlAttribute Lang = doc.CreateAttribute("Lang");
            Lang.Value = this._lang;
            newChild.Attributes.Append(Lang);

            XmlAttribute ASHOST = doc.CreateAttribute("ASHOST");
            ASHOST.Value = this._ASHOST;
            newChild.Attributes.Append(ASHOST);

            XmlAttribute SYSNR = doc.CreateAttribute("SYSNR");
            SYSNR.Value = this._SYSNR;
            newChild.Attributes.Append(SYSNR);
        }

        #endregion


    }
}
